/*
 *  Copyright © 2018 - 2021 xulianqiang90@163.com. All Rights Reserved.
 */

package io.light.frame.dal.mybatis.generator.core.mapper.func.auto;

import io.light.frame.dal.mybatis.generator.core.context.GenerationContext;
import io.light.frame.dal.mybatis.generator.core.domain.mapper.Mapper;
import io.light.frame.dal.mybatis.generator.core.domain.mapper.MapperFunc;
import io.light.frame.dal.mybatis.generator.core.mapper.func.injector.columns.ColumnsInjector;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Insert function auto builder
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-05-22 10:20
 */
@Component
public class InsertAutoBuilder extends FuncAutoBuilder {

    private final ColumnsInjector columnsInjector;

    public InsertAutoBuilder(ColumnsInjector columnsInjector) {
        this.columnsInjector = columnsInjector;
    }

    /**
     * Worked function type
     *
     * @return {@link MapperFunc.Type}
     */
    @Override
    public MapperFunc.Type funcType() {
        return MapperFunc.Type.insert;
    }

    /**
     * Build insert function content
     *
     * @param element    function element
     * @param mapperFunc {@link MapperFunc}
     * @param content    {@link MapperFunc.Content}
     */
    @Override
    public void build(Element element, MapperFunc mapperFunc, MapperFunc.Content content) {
        Mapper mapper = GenerationContext.current().getMapper();
        List<Mapper.Property> properties = mapper.getInsertProperties();
        columnsInjector.buildInsertBlock(content, mapper.getTable().getName(), properties);
    }
}
